{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f947babd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fitting(A,b):\n",
    "    return np.linalg.inv(A.T @ A) @ (A.T@ b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0c5df20f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calA(X):\n",
    "    A = np.zeros((X.size,2))\n",
    "    A[:,0] = 1\n",
    "    A[:,1] = X\n",
    "    return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a578e26a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calb(Y):\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c24c5b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b3590063",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.linspace(0,10,101)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c69d5ffe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,\n",
       "        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,\n",
       "        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,\n",
       "        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,\n",
       "        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,\n",
       "        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,\n",
       "        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,\n",
       "        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,\n",
       "        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,\n",
       "        9.9, 10. ])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c6fcd79a",
   "metadata": {},
   "outputs": [],
   "source": [
    "R = 1\n",
    "C = 1\n",
    "i = 5 / R * np.exp(- t / (R * C)) + (np.random.rand(t.size) - 0.5) * 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a02ff116",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\dell\\AppData\\Local\\Temp/ipykernel_400/4008166283.py:1: RuntimeWarning: invalid value encountered in log\n",
      "  Y = np.log(i)\n"
     ]
    }
   ],
   "source": [
    "Y = np.log(i)\n",
    "b = calb(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5abc4fba",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = calA(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a3b04309",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = fitting(A,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15bb4510",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
